import os,copy
from detectEnglish import removeNonLetters

def count_words(message = ''):
    
    message = removeNonLetters(message.upper())
    possiblewords = message.split()
    words_count = {}
    
    for word in possiblewords:
        if word in words_count:
            words_count[word]+=1
        else:
            words_count[word]=1
    
    return words_count

def remove_rare_words(words_count,count_rare = 1):
    
    ret = {}
    for word in words_count:
        if words_count[word]>count_rare:
            ret[word] = words_count[word]
            
    return ret

def solve(message = ''):
    
    words_count = count_words(message)
    words_count = remove_rare_words(words_count=words_count, count_rare=1)
    
    return ' '.join(sorted(words_count.keys()))

if __name__=='__main__':
    import pprint
    pprint.pprint(solve(open('sources/ciphertext','r').read()))
    
    